home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17796 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.1 KB  |  131 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!pladow
  3. From: pladow@netcom.com (Peter Ladow)
  4. Subject: Template woes
  5. Message-ID: <pladowDpztGt.Bvo@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. Date: Wed, 17 Apr 1996 06:17:16 GMT
  9. Sender: pladow@netcom22.netcom.com
  10.  
  11. I am using Borland C++ 4.0, and I'm trying to learn how to do data
  12. structures in C++.  I have taken a course in C (over 3 yrs ago however)
  13. so I understand the basics.  Right now I am trying to to a binary tree
  14. using templates.
  15.  
  16. Here is my interface (thus far):
  17.  
  18. tree.h:
  19. #include <iostream.h>
  20.  
  21. // Forward declaration
  22. template <class TYPE> class Tree;
  23.  
  24. template <class TYPE>
  25. class Leaf {
  26.    private:
  27.       friend class Tree<TYPE>;
  28.  
  29.       // Data members
  30.       Leaf<TYPE>* left;
  31.       Leaf<TYPE>* right;
  32.       TYPE data;
  33.  
  34.       // Constructor
  35.       Leaf(TYPE d, Leaf<TYPE>* l, Leaf<TYPE>* r) : data(d), right(r), left(l){}
  36. };
  37.  
  38. template <class TYPE>
  39. class Tree {
  40.    public:
  41.       // Constructor
  42.       Tree(void) {root = NULL; }
  43.  
  44.       // Access functions
  45.       TYPE find(TYPE d);
  46.       void insert(TYPE d);
  47.  
  48.    private:
  49.  
  50.       // Root node
  51.       Leaf<TYPE>* root;
  52.  
  53.       // Internal function
  54.       TYPE find(Leaf<TYPE>*r, TYPE d) const;
  55. };
  56.  
  57. Now the problem isn't during compilation.  When the linker trys to resolve
  58. symbols I get:
  59. Linker Error: Undefined symbol Tree<int>::insert(int) in module TEST.CPP
  60.  
  61. In my main() code I have:
  62.  
  63. .
  64. .
  65. .
  66.    Tree<int> test;
  67.    int a=5;
  68.  
  69.    test.insert(a);
  70. .
  71. .
  72. .
  73.  
  74. An interesting note is that I had my constructor for class Tree external
  75. and I got the same error.  But when I made it inline, it went away.
  76.  
  77. Here is my implementation of Test<TYPE>::insert(TYPE d);
  78.  
  79. tree.cpp:
  80. #include <string.h>
  81.  
  82. template <class TYPE>
  83. void Tree<TYPE>::insert(TYPE d) {
  84.     Leaf<TYPE>* temp=root;
  85.     Leaf<TYPE>* old;
  86.  
  87.     if (root == NULL) {
  88.         root=new Leaf<TYPE>(d, NULL, NULL);
  89.         return;
  90.     }
  91.  
  92.     while(temp != NULL) {
  93.         old=temp;
  94.         if ((temp->data, d) == 0) {
  95.             return;
  96.         }
  97.  
  98.         if (comp(temp->data, d) > 0)
  99.             temp=temp->left;
  100.  
  101.         else
  102.             temp=temp->right;
  103.     }
  104.  
  105.     if (comp(old->data, d) > 0)
  106.         old->left=new Leaf<TYPE>(d, NULL, NULL);
  107.     else
  108.         old->right=new Leaf<TYPE>(d, NULL, NULL);
  109. }
  110.  
  111. Now, while looking through the Programmers Guide that came with Borland,
  112. I found the example of the vector class (on page 165).  When I typed
  113. exactly what I read there, I got the same error for Vector<T>::Vector(int n);
  114.  
  115. Is this a bug?  Am I typing something wrong?  (Note I even tried a semicolon
  116. at the end of the insert() function like that in the book, but no luck.  That
  117. is just a syntax issue though.)
  118.  
  119. Note that in all these examples, the interface is separate from the
  120. implementation, i.e. in different source files.
  121.  
  122. Thanks a lot for you help.
  123.  
  124. Pete
  125. pladow@netcom.com
  126. ___________________________________________________________________________
  127. |                    | "To love for the sake of being loved is |
  128. |ORQ: And the meek shall        |  human, to love for the sake of loving  |
  129. |     inherit the earth...      |  is angelic." --Alphonse de Lamartine   |
  130. ---------------------------------------------------------------------------
  131.